package com.project.config;

import com.project.properties.AliOssProperties;
import com.project.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class OssConfiguration {
    /*
    * 解释一下为什么不直接在AliOssUtil中加@autowrid,因为加载顺序问题，spring会先把下边的upload加载完才执行上边的自动注入，导致创建的实例对象值都是null
    * */
    @Bean
    @ConditionalOnMissingBean//如果容器中不存在这个bean，则创建这个bean,只加载一次
    public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){
        log.info("开始创建阿里云文件上传客户端：{}",aliOssProperties);
        AliOssUtil aliOssUtil = new AliOssUtil(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret(), aliOssProperties.getBucketName());
        return aliOssUtil;
    }
}
